package com.controller.survey;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.controller.vo.CaseVO;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.CaseDiscribe;
import com.pojo.CaseDiscribeExample;
import com.pojo.Caseone;
import com.pojo.CaseoneExample;
import com.pojo.CaseoneExample.Criteria;
import com.pojo.Employee;
import com.pojo.Loss;
import com.pojo.OwnCheckImg;
import com.pojo.Policy;
import com.pojo.PolicyExample;
import com.service.casediscribeservice.CasediscribeService;
import com.service.caseoneservice.Caseoneservice;
import com.service.lossservice.Lossservice;
import com.service.ownCheckImg.OwnCheckImgService;
import com.service.policyidservice.Policyservice;

@Controller
public class SurveyController {
	    @Autowired
	    private Caseoneservice caseoneservice;
	    @Autowired
	    private OwnCheckImgService ownCheckImgService;
	    @Autowired
	    private Lossservice  Lossservice;
	    @Autowired
	    private CasediscribeService casediscribeService;
	    @Autowired
	    private Policyservice policyservice;
	    
	    
	    @RequestMapping("/demox")
	    public String test1(){
	        
	  
	        
	        return "survey_show";
	    }
	    
	    
	
	    @ResponseBody
		@RequestMapping(value = "survey_show_show.do", produces = { "application/json;charset=utf-8" })
	    public String test1(Integer pageNo){
	    	PageHelper.startPage(pageNo,5);
	    	List<CaseVO> caseVOList = caseoneservice.getCaseVOList();
	    	
	    	PageInfo<CaseVO> page = new PageInfo<CaseVO>(caseVOList);
	  
	        
	        return JSON.toJSONString(page);
	    }

	   
	    @RequestMapping("/demoy.html")
	    public String test(String id, Model model,HttpSession session){
	    	Employee employee = (Employee)session.getAttribute("loginEmployee");
	        
	    	System.out.println(employee.getEmployeename());
	    	
	    	
	    	CaseVO caseVO = caseoneservice.getCaseVO(id, employee);
	    	model.addAttribute("caseVO", caseVO);
	    	
	    	return "site_survey";
	    }
	    
	    @RequestMapping("/doSurvey")
	    public String doSurvey(Integer[] ids,String assessmentstatusonline,String caseno,String employeeid){
	       if (ids!=null&&ids.length>0) {
	    		for (Integer integer : ids) {
		    		
		    		OwnCheckImg ownCheckImg = ownCheckImgService.selectByPrimaryKey(integer);
		    		ownCheckImg.setStatus(0);
		    		ownCheckImgService.updateByPrimaryKey(ownCheckImg);
		    		
		    		
				}
	    		CaseoneExample caseoneExample = new CaseoneExample();
	    		Criteria criteria = caseoneExample.createCriteria();
	    		criteria.andCasenoEqualTo(caseno);
	    		List<Caseone> list = caseoneservice.selectByExample(caseoneExample);
	    		Caseone caseone = null;
	    		if (list!=null&&list.size()>0) {
	    			caseone=list.get(0);
				}
	    		caseone.setStatusid(5);
	    		caseone.setAssessmentstatusonline(0);
	    		caseone.setEmployeeid(Integer.parseInt(employeeid));
	    		caseone.setAssessmentstatusonline(Integer.parseInt(assessmentstatusonline));
	    		caseoneservice.updateByPrimaryKey(caseone);
	   }else{
		        CaseoneExample caseoneExample = new CaseoneExample();
   		        Criteria criteria = caseoneExample.createCriteria();
   		        criteria.andCasenoEqualTo(caseno);
   		        List<Caseone> list = caseoneservice.selectByExample(caseoneExample);
   		        Caseone caseone = null;
   		        if (list!=null&&list.size()>0) {
   			        caseone=list.get(0);
			    }
   		        caseone.setStatusid(6);
   		        caseone.setAssessmentstatusonline(1);
   		        caseone.setEmployeeid(Integer.parseInt(employeeid));
   		        caseone.setAssessmentstatusonline(Integer.parseInt(assessmentstatusonline));
   		        caseoneservice.updateByPrimaryKey(caseone);
		        Loss loss = new Loss(); 
   		        loss.setCaseno(caseone.getCaseno());
   		     CaseDiscribeExample caseDiscribeExample = new CaseDiscribeExample();
   		     com.pojo.CaseDiscribeExample.Criteria criteria2 = caseDiscribeExample.createCriteria();
   		     criteria2.andCasenoEqualTo(caseone.getCaseno());
   		     List<CaseDiscribe> list2 = casediscribeService.selectByExample(caseDiscribeExample);
   		     CaseDiscribe caseDiscribe=null;
   		     if (list2!=null&&list2.size()>0) {
			       caseDiscribe = list2.get(0);
			
		}
   		        
   		String identity = caseDiscribe.getIdentity();
   		PolicyExample policyExample = new PolicyExample();
   		com.pojo.PolicyExample.Criteria criteria3 = policyExample.createCriteria();
   		criteria3.andIdentityEqualTo(identity);
   		criteria3.andInsuranceidEqualTo(1);
   		List<Policy> list3 = policyservice.selectByExample(policyExample);
   		Policy policy =null;
   		if (list3!=null&&list3.size()>0) {
		 policy = list3.get(0);
		}
   		loss.setPolicyno(policy.getPolicyno());
   		Lossservice.insert(loss);
   		
		   
	   }
	    	
	    	
	    	
	    	
	    	return "redirect:demox.do";
	    }
	    
	
}
